﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;


namespace Appcode
{
    public class ProductsDAL
    {

        private MyConnection DB_Con = new MyConnection();

        public void SaveUpload(Images img)
        {
            try
            {
                SqlConnection con = DB_Con.openConnection();
                SqlCommand com = new SqlCommand("saveImgUpload", con);
                com.Parameters.AddWithValue("@abname", img.AlbumName);
                com.Parameters.AddWithValue("@imgname", img.ImgName);
                com.Parameters.AddWithValue("@imgpath", img.ImgPath);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                com.ExecuteNonQuery();
            }
            catch (Exception)
            {

            }
        }

        public List<String> getALbumName(int _uid)
        {
            try
            {
                SqlConnection con = DB_Con.openConnection();
                SqlCommand com = new SqlCommand("getAlbumName", con);
                com.Parameters.AddWithValue("@uid", _uid);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                SqlDataReader reader = com.ExecuteReader();
                List<String> album = new List<string>();
                while (reader.Read())
                {
                    album.Add(reader.GetString(0));
                }
                return album;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public void createAlbumName(int _uid, String name)
        {
            try
            {
                SqlConnection con = DB_Con.openConnection();
                SqlCommand com = new SqlCommand("createAlbumName", con);
                com.Parameters.AddWithValue("@uid", _uid);
                com.Parameters.AddWithValue("@name", name);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                com.ExecuteNonQuery();
            }
            catch (Exception)
            {

            }
        }

        public List<Images> getAllImagesByUID(int uid)
        {
            try
            {
                SqlConnection con = DB_Con.openConnection();
                SqlCommand com = new SqlCommand("getAllImagesByUID", con);
                com.Parameters.AddWithValue("@uid", uid);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                SqlDataReader reader = com.ExecuteReader();
                List<Images> listImg = new List<Images>();
                while (reader.Read())
                {
                    Images img = new Images();
                    img.ImageID = reader.GetInt32(0);
                    img.AlbumName = reader.GetString(1);
                    img.ImgName = reader.GetString(2);
                    img.ImgPath = reader.GetString(3);
                    listImg.Add(img);
                }
                return listImg;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<String> getProductsSize()
        {
            try
            {
                SqlConnection con = DB_Con.openConnection();
                SqlCommand com = new SqlCommand("getProductSize", con);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                SqlDataReader reader = com.ExecuteReader();
                List<String> listSize = new List<String>();
                while (reader.Read())
                {
                    String w = reader.GetInt32(1).ToString();
                    String h = reader.GetInt32(2).ToString();
                    String s = w + "x" + h;
                    listSize.Add(s);
                }
                return listSize;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public float getCostOfSize(int sWidth, int sHeight)
        {
            try
            {
                SqlConnection con = DB_Con.openConnection();
                SqlCommand com = new SqlCommand("getCostOfImgSizeBySizes", con);
                com.Parameters.AddWithValue("@sWith", sWidth);
                com.Parameters.AddWithValue("@sHeight", sHeight);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                SqlDataReader reader = com.ExecuteReader();
                float price = 0;
                while (reader.Read())
                {
                    price = float.Parse(Convert.ToString(reader.GetValue(0)));
                }
                return price;
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}

